package com.hdx.server.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


import com.hdx.common.constant.MessageConstant;
import com.hdx.common.constant.StatusConstant;
import com.hdx.common.exception.DeletionNotAllowedException;
import com.hdx.common.result.PageResult;
import com.hdx.model.dto.CategoryDTO;
import com.hdx.model.dto.CategoryPageQueryDTO;
import com.hdx.model.entity.Category;
import com.hdx.server.mapper.CategoryMapper;
import com.hdx.server.mapper.DishMapper;
import com.hdx.server.mapper.SetmealMapper;
import com.hdx.server.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * 分类业务层
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;








    //根据类型查询分类(菜品管理查询框的菜品分类/套餐管理查询框的套餐分类)
    //根据类型查询所以分类

    //分类分页查询
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //Pagehelper
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        //Page
        Page<Category> page=categoryMapper.pageQuery(categoryPageQueryDTO);
        //PageResult(total,records)
        return new PageResult(page.getTotal(),page.getResult());
    }

    //新增分类
    @Override
    public void save(CategoryDTO categoryDTO) {
        //设置分类状态为禁用
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(StatusConstant.DISABLE);
        //插入数据库
        categoryMapper.insert(category);
    }


    //根据id删除分类
    @Override
    public void deleteById(Long id) {
        //抛出异常:1.查询当前分类是否关联了菜品，如果关联了就抛出业务异常 2.查询当前分类是否关联套餐,如果关联了就抛出业务异常
        Integer dishCount=dishMapper.getCountByCategoryId(id);
        if(dishCount>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        Integer setmealCount=setmealMapper.getCountByCategoryId(id);
        if(setmealCount>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //删除数据库分类
        categoryMapper.deleteById(id);


    }
    //修改分类
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        categoryMapper.update(category);
    }

    //启用禁用分类
    @Override
    public void startOrStop(Integer status, Long id) {
        //build()
        Category category = Category.builder()
                .id(id)
                .status(status)
                .build();
        categoryMapper.update(category);



    }

    @Override
    public List<Category> getCategoryByType(Integer type) {
        List<Category> categoryList=categoryMapper.getCategoryByType(type);
        return  categoryList;
    }
}

